package CollectionDemo.QueueImplement;
//会造成浪费
public class MyQueue implements IQueue {
    private int[] arr = new int[10];
    //头指针
    private int front = 0;
    //尾指针
    private int rear = 0;
    @Override
    public void offer(Integer integer) {
        if (!isFull()) {
            arr[rear++] = integer;
            System.out.println("插入成功");
        } else {
            System.out.println("插入失败");
        }

    }

    @Override
    public void poll() {
        if (!isEmpty()) {
            System.out.println("队列头：" + arr[front++] + "被移出");
        } else {
            System.out.println("请先插入数据");
        }
    }

    @Override
    public boolean isEmpty() {
        return front == rear;
    }

    @Override
    public boolean isFull() {
        return rear == arr.length;
    }

    @Override
    public String toString() {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("[");
        for (int i = rear - 1; i >= front; i--) {
            stringBuffer.append(arr[i]);
            stringBuffer.append(" ");
        }
        stringBuffer.append("]");
        return stringBuffer.toString();
    }
}
